const MYJSON = {
  stringify(object){
    const checkType = (val)=>{
      if(typeof val  ==='string' || typeof val  ==='number'){
        return true;
      }else{
        return false
      }
    }

    const run  = (obj)=>{
      let string = '';
      if(Array.isArray(obj)){
        string += '['
        obj.forEach((item)=>{
          // string += run(item);
          if(checkType(item)){
            string += `"${item}",`
          }else{
            string += run(item) +','
          }
        })
        string = string.slice(0,string.length - 1)
        string += ']'
      }else if(Object.prototype.toString.call(obj) === '[object Object]'){
        string += '{'
        for(let key in obj){
          const value = obj[key]
          if(checkType(value)){
            string += `"${key}":"${value}",`
          }else{
            string += `"${key}":${run(value)},`
          }
        }
        string = string.slice(0,string.length - 1)
        string += '}'
      }
      
      return string
    }
    return run(object);
  },
  parse(string){
    let str = string;
    let index = 0;
    let stack = []
    
    const objBeginReg = /^{/
    const objEndReg = /^}/
    const arrBeginReg = /^\[/
    const arrEndReg = /^\]/
    const keyReg = /^"(\w+)"\s*?:/
    const strReg1 = /^"[^"]*"/
    const strReg2 = /^'[^']*'/
    const numReg = /^\d+/

    const eat = (num)=>{
      str = str.slice(num)
      index += num;
    }
    const formatString = (value)=>{
      value = value.slice(1);
      value = value.slice(0,value.length - 1)
      return value;
    }
    const matchToString = (value)=>{
      let newVal
      newVal = value.slice(1)
      const reg =  /"(\s*?):/
      newVal = newVal.replace(reg,'')
      return newVal;
    }
   let that = {}
   let key; //如果key存在，代表上级是{} 否则默认为[]
    while(str.length >0 && index < string.length){
      if(objBeginReg.test(str)){
        if(stack.length === 0){
          that = {}
          stack.push(that);
        }else if(key){
          const obj = stack[stack.length - 1];
          // if(Array.isArray(obj)){
          //   that = {}
          //   obj.push(that)
          //   stack.push(that);
          // }else{
          //   that = {};
          //   stack.push(that);
          //   obj[key] = that
          // }
          that = {};
          stack.push(that);
          obj[key] = that
          
        }else if(!key){
          const obj = stack[stack.length - 1];
          that = {}
          obj.push(that)
          stack.push(that);
        }
        eat(1)
        
      }else if(objEndReg.test(str)){
        const obj = stack.pop()
        // console.log(stack);
        if(stack.length === 0){
          return obj
        }
        eat(1)
        key = undefined;
      }else if(arrBeginReg.test(str)){
        if(stack.length === 0){
          that = []
          stack.push(that);
        }else if(key){
          const obj = stack[stack.length - 1];
          // if(Array.isArray(obj)){
          //   that = []
          //   obj.push(that)
          //   stack.push(that);
          // }else{
          //   that = [];
          //   stack.push(that);
          //   obj[key] = that
          // }
          that = [];
          stack.push(that);
          obj[key] = that
        }else if(!key){
          const obj = stack[stack.length - 1];
          that = []
          obj.push(that)
          stack.push(that);
        }
        eat(1)
        key = undefined;
      }else if(arrEndReg.test(str)){
        const obj = stack.pop()
        if(stack.length === 0){
          return obj
        }
        eat(1)
        key = undefined;
      }else if(keyReg.test(str)){
        const matched = keyReg.exec(str);
        const obj = stack[stack.length - 1];
        key = matchToString(matched[0]);
        // that[key] = {}
        eat(matched[0].length)
      }else if(strReg1.test(str) || strReg2.test(str)){
        let matched;
        matched = strReg1.exec(str)=== null ? strReg2.exec(str) : strReg1.exec(str);
        const obj = stack[stack.length - 1];
        const value = formatString(matched[0])
        if(Array.isArray(obj)){
          obj.push(value)
        }else{
          obj[key] = value
        }
        eat(matched[0].length)
        key = undefined;
      }else if(numReg.test(str)){
        let matched = numReg.exec(str)
        const obj = stack[stack.length - 1];
        const value =  Number(matched[0])
        if(Array.isArray(obj)){
          obj.push(value)
        }else{
          obj[key] = value
        }
        eat(matched[0].length)
        key = undefined;
      }else{
        eat(1);
      }
    }
  }

}
module.exports = MYJSON

// const asd = {
//   a:'aa',
//   bb:{
//     bb1:'bbvalyue'
//   },
//   cc:[
//     1,2,'stgr'
//   ],
//   qq:[
//     {
//       type:'dengy',
//       name:'vanamm'
//     }
//   ],
//   xa:123,
//   ar:[
//     [
//       12,33
//     ]
//   ]
// }
// let str = JSON.stringify(asd)
// const code = `
// {"a"  :"aa","bb":{"bb1":"bbvalyue"},"cc":[1,2,"stgr"],"qq"  :  [{"type":"dengy","name":"vanamm"}],"xa":123,"ar":[[  12,  33]]}
// `

// console.log(MYJSON.parse(code));

// var str = new String("ha")
// console.log(typeof str);
// console.log(asd.valueOf())
